package com.pjc.dao;

import com.pjc.db.RedisDB;
import com.pjc.model.UserStatus;
import com.pjc.utils.MsgPackUtils;

public class UserStatusDao {


	/**
	 * 添加用户信息到缓存
	 * @param userStatus
	 * @param keyVal
	 * @throws Exception
	 */
	public static void regist(UserStatus userStatus, String keyVal) throws Exception {
		byte[] key = (keyVal + userStatus.getUserId()).getBytes();
		byte[] val = MsgPackUtils.getMsgPack().write(userStatus);
		RedisDB.getInstance().setData(key, val);
	}
    /**
     * 注销用户缓存(设置默认0)
     * @param userId
     * @param keyVal
     * @throws Exception
     */
	public static void unRegist(String userId, String keyVal) throws Exception {
		UserStatus userStatus = getUserStatus(userId, keyVal);
		if (userStatus == null)
			return;
		userStatus.setServerId(0);
		userStatus.setUuid("");
		regist(userStatus, keyVal);
	}
    /**
     * 获取用户对象
     * @param userId
     * @param keyVal
     * @return
     * @throws Exception
     */
	public static UserStatus getUserStatus(String userId, String keyVal) throws Exception {
		byte[] key = (keyVal + userId).getBytes();
		byte[] val = RedisDB.getInstance().getData(key);
		if ((val == null) || (val.length == 0))
			return null;
		return (UserStatus) MsgPackUtils.getMsgPack().read(val, UserStatus.class);
	}

}
